var utils = {
  toImgUrl: function (url) {
    return url;
  },
  getUrlParam: function (key, val) {
    var url = window.location.href;
    var type = typeof val;
    var params = url.substr(url.indexOf("?") + 1).split("&");
    if (params != null) {
      for (var i = 0; i < params.length; i++) {
        var strs = params[i].split("=");
        if (strs[0] == key) {
          val = strs[1];
          break;
        }
      }
    }
    return type == "number" ? parseInt(val) : String(val);
  },
  toggleClass: function (dom, className) {
    if (!dom) return
    className = className ? className : 'focus'
    $(".item").removeClass(className)
    $(dom).addClass(className)
  },
  getCurInfo: function () {
    var focus = $(".focus");
    var id = focus.attr("id");
    if (!id) return
    var lastIndex = id.lastIndexOf('-')
    var index = id.substring(lastIndex + 1)
    var type = id.substring(0, lastIndex)
    return {
      index: index,
      type: type,
      id: id
    }
  },
  scale: function(el, multiple){
    if(!el.length) return
    var left = el.offset().left
    var top = el.offset().top
    var width = el.width()
    var height = el.height()
    var pointX = left + width / 2
    var pointY = top + height / 2
    var scaleW = width * multiple
    var scaleH = height * multiple
    el.css({
      position: 'relative',
      width: scaleW,
      height: scaleH,
      top: pointY - scaleH / 2 ,
      left: pointX - scaleW / 2
    })
  },
  formatTime: function(time) {
    var time = parseInt(time || 0)
    var h = parseInt(time / 3600)
    var m = parseInt((time % 3600) / 60)
    var s = time % 60
    h = h < 10 ? ('0' + h) : h 
    m = m < 10 ? ('0' + m) : m 
    s = s < 10 ? ('0' + s) : s 
    return h + ':' + m + ':' + s
  },
  auth: function (mediaCode) {
    var _mediaCode = mediaCode ? mediaCode : config.mediaCode
    var _data;
    if (!this.isSTB()) {
      page.isSub = 1
      return
    }
    var url = page.urls.domain + "/datajsp/au_PlayFilmData.jsp?mediaCode=" + _mediaCode;
    if(config.getOperators() == 'dx'){
      url = url+"&productLevel=4"
    }
    $.ajax({
      type: "get",
      async: false,
      url: url,
      xhrFields: {
        withCredentials: true
      },
      crossDomain: true,
      success: function (data) {
        _data = eval("(" + data + ")");
      }
    })
    page.productList = _data.productObject.productList
    page.isSub = _data.forwardJson.isSub
    page.forwardUrl = _data.forwardJson.forwardUrl    
  },
  order: function () {
    var forwardUrl = page.forwardUrl || '/'
    var home = page.urls._domain
    if (page.isSub == 0) {
        dialog.show('您已订购，无需重复订购！')
        return;
    }
    if (!this.isSTB()) {
      console.log(home.substring(0, home.indexOf('?')) + '?spVodPlayUrl=' + encodeURIComponent(forwardUrl))
      dialog.show('跳转局方订购页面...')
    } else {
      try {
        video.destroy()
      } catch (error) {
        
      }
      var reg = /(elId=)(.+?&)/g
      var info = this.getCurInfo()
      
      var from = location.href;
      var str ="";
      var strArr = [];
      var tar = "";
      if (location.search){
        str = location.search.split("?")[1];
        if(str.indexOf("elId=") > -1){
          strArr = str.split("&");
          for(var i = 0; i < strArr.length; i++){
            if(strArr[i].indexOf("elId") > -1){
               tar = strArr[i].split("=")[1];
               break;
            }
          }
          from = from.replace(tar,info.id)
        }else{
          from = from + '&elId=' + info.id;
        }  
      }else{
        from = from + '?elId=' + info.id;
      }

      // try {
      //   if (from.match(reg)) {
      //     from = (info && info.id) ? from.replace(reg, 'elId=' + info.id + '&' ) : from.replace(reg, '')  
      //   } else {
      //     from = (info && info.id) ? from + '&elId=' + info.id  : from
      //   }
      // } catch (error) {
        
      // }

      try {
        if (typeof education !== "undefined") {
          from += '&page=' + education.page
        }
      } catch (error) {
        
      }
      var url =  forwardUrl + '&returnUrl=' + escape(from + '&Result=0') + '&backUrl='  + escape(from + '&Result=1')
      if(home.indexOf("?") > 0 ){
        home = home.substring(0, home.indexOf('?'));
      }
      window.location.href= home + '?spVodPlayUrl=' + encodeURIComponent(url)
    }
  },
  getPlayUrl: function (code) {
    var url = ''
    if (!this.isSTB() || !code) return url
    $.ajax({
      type: "get",
      url: page.urls.domain + '/datajsp/play_getVodPlayUrl.jsp?mediaCode=' + code,
      xhrFields: {
          withCredentials: true
      },
      async: false,
      crossDomain: true,
      success: function(data) {
          var data = eval('(' + data + ')');
          if (data.playUrl) {
            url = data.playUrl
          }
      },
      error: function(err) {
        url = ''
      }
    })
    return url
  },
  /**
   * 
   * @returns boolean 返回是不是机顶盒标识
   */
  isSTB: function () {
    var flag
    try {
      Authentication.CUGetConfig('UserID')
      flag = true
    } catch (error) {
      flag = false
    }
    return flag
  },
  useStatus: function (ctype) {
    ctype = ctype ? ctype : 'default'
    return '<div class="flag flag-' + ctype + '"></div>'
  }
};

